package com.example.a12.adapter;


import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import androidx.recyclerview.widget.RecyclerView;

import com.example.a12.R;
import com.example.a12.bean.MusicMessage;

import java.util.ArrayList;

public class MusicAdapter extends RecyclerView.Adapter<MusicAdapter.messageViewHolder> {
    private ArrayList<MusicMessage> list = new ArrayList<>();
    private Context context;
    //将消息集合和上下文作为构造函数的形参快速创建适配器
    public MusicAdapter(ArrayList<MusicMessage> list, Context context) {
        this.list = list;
        this.context = context;
    }
    @NonNull
    @Override
    public messageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
            viewType) {
// 返回要显示的视图（消息模板）
        View view = LayoutInflater.from(context).inflate(R.layout.music_item,
                parent, false);
        return new messageViewHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull messageViewHolder holder, int
            position) {
// 完成消息模板中控件的绑定并设置对应的字段
        messageViewHolder messageViewHolder = (messageViewHolder) holder;
        MusicMessage userMessage = list.get(position);
        int icon = userMessage.getIcon();
        String name = userMessage.getName();
        String content = userMessage.getContent();
        String time1 = userMessage.getTime1();
        String time2 = userMessage.getTime2();
        messageViewHolder.icon.setBackgroundResource(icon);
        messageViewHolder.name.setText(name);
        messageViewHolder.content.setText(content);
        messageViewHolder.time1.setText(time1);
        messageViewHolder.time2.setText(time2);
    }
    @Override
    public int getItemCount() {
        return list.size();
    }
    public class messageViewHolder extends RecyclerView.ViewHolder {
        ImageView icon;
        TextView name,content,time1,time2;

        public messageViewHolder(@NonNull View itemView) {
            super(itemView);
            icon =  itemView.findViewById(R.id.img1);
            name = itemView.findViewById(R.id.title);
            content =  itemView.findViewById(R.id.content);
            time1 =  itemView.findViewById(R.id.time1);
            time2 =  itemView.findViewById(R.id.time2);
        }
    }
}
